﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Data.Sql;
using MYNAMESPACE;

namespace final_project
{
    public partial class ATM_Login : Form
    {
        private connect_db con;
     
        

        
        public ATM_Login()
        {
          
            InitializeComponent();
        }

        private void Login_Click(object sender, EventArgs e)
        {
            // Validating Start code

            if (this.ValidateChildren(ValidationConstraints.Enabled))
            {
                MessageBox.Show("All controls are valid!");
                //Logic to save...
            }
            else
            {
                MessageBox.Show("There are invalid controls on the form.");
                //Return user to form...
            }

            //Validating end code 


            
            user us = new user();
            


            if (ID.Text.Length == null || Password.Text.Length == null)
            {
                MessageBox.Show("must have value");

            }
            else
            {
                us.setnational_id(int.Parse(ID.Text.Trim()));
                us.setpassword(string.Format(Password.Text.Trim()));
            
                con = new connect_db();
                con.sqlquery("select * from userdata where id= (@namep) and pass=(@namep1)");
                con.cmd.Parameters.Add("@namep", us.getnational_id());
                con.cmd.Parameters.Add("@namep1", us.getpassword());
                con.sqlnonex();
                int c = 0;
                foreach (DataRow dr in con.QueryEx().Rows)
                {
                    c = c + 1;
                }
                if (c == 1)
                {
                    this.Hide();
                    ATM_Account AA = new ATM_Account();
                    AA.passvalue = ID.Text;
                    AA.ShowDialog();
                    
                   

                }
                else
                {
                    MessageBox.Show("not correct");
                }
        
    

            }
        }

        private void ATM_Login_Load(object sender, EventArgs e)
        {
            
        }

        private void Exit_Click(object sender, EventArgs e)
        {
            this.Hide();
            Home H = new Home();
            H.ShowDialog();

        }

        private void ID_Validating(object sender, CancelEventArgs e)
        {

            if (string.IsNullOrEmpty(ID.Text))
            {

                e.Cancel = true;
                ID.Focus();
                errorProvider1.SetError(ID, "ID Must Be Entered !");

            }

            else if (!Regex.IsMatch(ID.Text, @"^\d{4}$"))
            {
                e.Cancel = true;
                errorProvider1.SetError(ID, "Valid ID 4 Number");

            }



            else if (Regex.IsMatch(ID.Text, @"^\d{4}$"))
            {
                e.Cancel = false;
                errorProvider1.SetError(ID, "");

            }
            else { }

        }

        private void Password_Validating(object sender, CancelEventArgs e)
        {

            if (string.IsNullOrEmpty(Password.Text))
            {

                e.Cancel = true;
                Password.Focus();
                errorProvider1.SetError(Password, "Password Must Be Entered !");
                //passworderror.SetError(Password_textBox.Text, "Password required!");
            }
            else if (!Regex.IsMatch(Password.Text, @"^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!*@#$%^&+=]).*$"))
            {
                e.Cancel = true;
                errorProvider1.SetError(Password, "Password Must more Than 8 Digits Contain upper-lowercase,numbers and special characters");

            }
            else
            {
                e.Cancel = false;
                errorProvider1.SetError(Password, "");
            }



        }
           
        }
    }

